package com.lsk.mobile;

import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.lsk.util.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class DouYinService {
    /**
     * 支付宝会员处理
     */
    Cache cache= Redis.use();

    public ReturnData apiupMemberService(String code,String nick_name2){

        String appid="ttf16887d56e427e3e";

        String secret="42a0802c8834beecd6fcf033c008cb48d51f1690";

        String url="https://developer.toutiao.com/api/apps/jscode2session";

        Map<String,String> parameters=new HashMap<>();

        parameters.put("appid",appid);

        parameters.put("secret",secret);

        parameters.put("code",code);

        String resultStr=HttpKit.get(url,parameters);

        Syslog.info("返回消息为："+resultStr);

        Map<String,Object> restltMap=JsonUtil.getMapFromJsonObjStr(resultStr);



        final ReturnData returnData=new ReturnData(true);


        final String douyinId=restltMap.get("openid")+"";

        String sql="SELECT *from sys_user where id='"+douyinId+"'";

        Record sysUser= Db.findFirst(sql);

        if (sysUser==null){

            final String userId= douyinId;
            final String salt = UuidUtil.get6UUID();  //盐值
            final String encodePassword = MD5.md5("123456" + salt); //password+salt 使用md加密后
            final String nick_name=nick_name2;
            //开启事务
            Db.tx(new IAtom() {
                @Override
                public boolean run() throws SQLException {
                    String userNameRandom = RadomUtil.getName();//生成一个随机名字
                    try {
                        Record sysUser=new Record().set("id", userId).set("userName", nick_name)
                                .set("userCode", "ali"+ Tools.getRandomNum()+Tools.getRandomNum()).set("password", encodePassword).set("salt", salt)
                                .set("locked", 0).set("type", 4).set("alipayId",douyinId);

                        Db.save("sys_user", sysUser);

                        Record memberInfo=new Record().set("id", UuidUtil.get32UUID()).set("source",2)
                                .set("alipayId",douyinId)
                                .set("userId", userId).set("type",4).set("name",nick_name);

                        Db.save("yx_member", memberInfo);

                        String token = AES.AESEncode(userId);

                        Map<String, Record> userMap = new HashMap<>();

                        userMap.put("memberInfo", memberInfo);

                        cache.setex(token, 60 * 60 * 24, userMap);

                        Map result=new HashMap();

                        result.put("memberInfo",memberInfo);

                        result.put("token",token);

                        returnData.setResult(true);
                        returnData.setData(result);

                    } catch (Exception e) {

                        Syslog.error(e);
                        return false;//回滚
                    }
                    return true;
                }
            });

        }else {

            String id=sysUser.getStr("id");

            String memberSql="SELECT *from yx_member where userId='"+id+"'";

            Record record=Db.findFirst(memberSql);


            String token = AES.AESEncode(id);

            Map<String, Record> userMap = new HashMap<>();

            userMap.put("memberInfo", record);

            cache.setex(token, 60 * 60 * 24, userMap);

            Map result=new HashMap();

            result.put("memberInfo",record);

            result.put("token",token);

            returnData.setResult(true);

            returnData.setData(result);
        }

        return returnData;

    }
}
